﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;


namespace Web.frontStage
{
    public partial class regist : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        static private string GetConnectionString()
        {
            // To avoid storing the connection string in your code, 
            // you can retrieve it from a configuration file.
            return "Data Source=WEIWANG-PC\\SQLEXPRESS;Initial Catalog=iMovie;Integrated Security=True";
        }

        protected void Button1_Click(object sender, EventArgs e)
        {


          string checknumber =  validateNumber.Text.Trim();
          if (checknumber.ToLower().Equals(Session["CheckCode"].ToString().ToLower()))
          {




               string connectionString = GetConnectionString();
               string sqlString;

               SqlConnection sqlCon = new SqlConnection(connectionString);


               string userName = username.Text.Trim();
               string passWord = password.Text.Trim();
               string passwordAgain = passwordagain.Text.Trim();
               string selectedValue = secretQuestion.SelectedValue;
               string safeQue = "";
               switch (int.Parse(selectedValue))
               {
                   case 1: safeQue = "您母亲的姓名是？"; break;
                   case 2: safeQue = "您父亲的姓名是？"; break;
                   case 3: safeQue = "您母亲的生日是？"; break;
                   case 4: safeQue = "您父亲的生日是？"; break;
                   case 5: safeQue = "您的生日是？"; break;
               }
               string safeAns = secretanswer.Text.Trim();
               string userEmail = email.Text.Trim();
               string sex;
               if (sexMan.Checked == true)
               {
                   sex = "0";
               }
               else
               {
                   sex = "1";
               }
               ;
               string userRegTime = DateTime.Now.ToString();
               int userState = 1;

               string sqlString1 = "select * from UserDetail_Tab";
               SqlCommand command1 = new SqlCommand(sqlString1, sqlCon);
               SqlDataAdapter myAdapter = new SqlDataAdapter();
               myAdapter.SelectCommand = command1;
               sqlCon.Open();
               myAdapter.SelectCommand.ExecuteNonQuery();
               sqlCon.Close();
               DataSet myDataSet = new DataSet();
               myAdapter.Fill(myDataSet);
               int userid;

               if (myDataSet.Tables[0].Rows.Count == 0)
               {
                   userid = 1;
               }
               else
               {

                   userid = int.Parse(myDataSet.Tables[0].Rows[0]["user_id"].ToString());
                   for (int i = 0; i < myDataSet.Tables[0].Rows.Count; i++)
                   {
                       if (int.Parse(myDataSet.Tables[0].Rows[i]["user_id"].ToString()) > userid)
                           userid = int.Parse(myDataSet.Tables[0].Rows[i]["user_id"].ToString());
                   }
                   userid = userid + 1;

               }


               sqlString = " insert into UserDetail_Tab (user_id,UserDetail_name,UserDetail_pwd,UserDetail_email,UserDetail_safeQue,UserDetail_safeAns,UserDetail_regDate,UserDetail_state,UserDetail_sex) ";
               sqlString += " values ( ";

               sqlString += userid.ToString();
               sqlString += ",";
               sqlString += "'" + userName + "'";
               sqlString += ",";
               sqlString += "'" + passWord + "'";
               sqlString += ",";
               sqlString += "'" + userEmail + "'";
               sqlString += ",";
               sqlString += "'" + safeQue + "'";
               sqlString += ",";
               sqlString += "'" + safeAns + "'";
               sqlString += ",";
               sqlString += "'" + userRegTime + "'";
               sqlString += ",";
               sqlString += "3";
               sqlString += ",";
               sqlString += "'" + sex + "'";


               sqlString += " ) ";
               SqlCommand command2 = new SqlCommand(sqlString, sqlCon);
               sqlCon.Open();
               command2.ExecuteNonQuery();
               sqlCon.Close();
     //          Response.Write("<script>alert('用户创建成功O(∩_∩)O~')</script>");
     //          Response.Close();
              Response.Redirect("/frontStage/index.aspx");  

         }
          else
          { 
               Response.Write("<script>alert('验证码错误O(∩_∩)O~')</script>");
      //         Response.Close();
         
     //         Response.Redirect("/frontStage/regist.aspx");              
           }
              
         

          }

      
    }
}
    
